﻿using ComponentFactory.Krypton.Toolkit;
using SRB.Tg.Model;
using SRB.Tg.Operate;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Teigha.DatabaseServices;

namespace SRB.Tg.Cad
{
    public partial class PileCapInfoForm : KryptonForm
    {
        DataRow _row;
        double _scale;
        private static PileCapInfoForm _instance;
        public static PileCapInfoForm getInstance(DataRow row, double scale)
        {
            if (_instance == null)
            {
                _instance = new PileCapInfoForm(row,scale);
            }
            return _instance;
        }

        public PileCapInfoForm()
        {
            InitializeComponent();
        }
        public PileCapInfoForm(DataRow row, double scale)
        {
            InitializeComponent();
            _row = row;
            _scale = scale;
            this.configure_name.Text = row["configure_name"].ToString();
            this.around_l_reinforcement_diameter.Text = row["around_l_reinforcement_diameter"].ToString();
            this.around_reinforcement_spacing.Text = row["around_reinforcement_spacing"].ToString();
            this.around_w_reinforcement_diameter.Text = row["around_w_reinforcement_diameter"].ToString();
            this.around_h_reinforcement_diameter.Text = row["around_h_reinforcement_diameter"].ToString();
            this.around_h_reinforcement_spacing.Text = row["around_h_reinforcement_spacing"].ToString();
            this.bottom_l_reinforcement_diameter.Text = row["bottom_l_reinforcement_diameter"].ToString();
            this.bottom_l_reinforcement_spacing.Text = row["bottom_l_reinforcement_spacing"].ToString();
            this.bottom_reinforcement_layers_number.Text = row["bottom_reinforcement_layers_number"].ToString();
            this.bottom_w_reinforcement_diameter.Text = row["bottom_w_reinforcement_diameter"].ToString();
            this.bottom_w_reinforcement_spacing.Text = row["bottom_w_reinforcement_spacing"].ToString();        
            this.concrete_no.Text = row["concrete_no"].ToString();
            this.cross_section.Text = row["cross_section"].ToString();
            this.concrete_protect_layer.Text = row["concrete_protect_layer"].ToString();
            this.connector_spacing.Text = row["connector_spacing"].ToString();
            this.erection_reinforcement_diameter.Text = row["erection_reinforcement_diameter"].ToString();
            this.erection_reinforcement_spacing.Text = row["erection_reinforcement_spacing"].ToString();
            this.horizontal_support_l_reinforcement_diameter.Text = row["horizontal_support_l_reinforcement_diameter"].ToString();
            this.horizontal_support_reinforcement_layers_number.Text = row["horizontal_support_reinforcement_layers_number"].ToString();
            this.horizontal_support_reinforcement_spacing.Text = row["horizontal_support_reinforcement_spacing"].ToString();
            this.horizontal_support_w_reinforcement_diameter.Text = row["horizontal_support_w_reinforcement_diameter"].ToString();
            this.length.Text = row["length"].ToString();
            this.width.Text = row["width"].ToString();
            this.height.Text = row["height"].ToString();
            this.pile_penetration_depth.Text = row["pile_penetration_depth"].ToString();
            this.reinforcement_grade.Text = row["reinforcement_grade"].ToString();
            this.reinforcement_net_l_diameter.Text = row["reinforcement_net_l_diameter"].ToString();
            this.reinforcement_net_l_spacing.Text = row["reinforcement_net_l_spacing"].ToString();
            this.reinforcement_net_w_diameter.Text = row["reinforcement_net_w_diameter"].ToString();
            this.reinforcement_net_w_spacing.Text = row["reinforcement_net_w_spacing"].ToString();
            this.structure_number.Text = row["structure_number"].ToString();
            this.structure_type.Text = row["structure_type"].ToString();
            this.top_l_reinforcement_diameter.Text = row["top_l_reinforcement_diameter"].ToString();
            this.top_l_reinforcement_spacing.Text = row["top_l_reinforcement_spacing"].ToString();
            this.top_reinforcement_layers_number.Text = row["top_reinforcement_layers_number"].ToString();
            this.top_w_reinforcement_diameter.Text = row["top_w_reinforcement_diameter"].ToString();
            this.top_w_reinforcement_spacing.Text = row["top_w_reinforcement_spacing"].ToString();
           
        }

        public void SavModify()
        {
            if (_row != null)
            {
                if (!_row.IsNull("id"))
                {
                    string updatesql = string.Format("update t_cap_reinforcement set around_h_reinforcement_diameter='{1}',"
                        + "around_h_reinforcement_spacing='{2}',around_l_reinforcement_diameter='{3}',around_reinforcement_spacing='{4}',around_w_reinforcement_diameter='{5}',"
                        + "bottom_l_reinforcement_diameter='{6}',bottom_l_reinforcement_spacing='{7}',bottom_reinforcement_layers_number='{8}',bottom_w_reinforcement_diameter='{9}',"
                        + "bottom_w_reinforcement_spacing='{10}',concrete_no='{11}',concrete_protect_layer='{12}',connector_spacing='{13}',"
                        + "erection_reinforcement_diameter='{14}',erection_reinforcement_spacing='{15}',height='{16}',horizontal_support_l_reinforcement_diameter='{17}',"
                        + "horizontal_support_reinforcement_layers_number='{18}',horizontal_support_reinforcement_spacing='{19}',horizontal_support_w_reinforcement_diameter='{20}',length='{21}',"
                        + "pile_penetration_depth='{22}',reinforcement_grade='{23}',reinforcement_net_l_diameter='{24}',reinforcement_net_l_spacing='{25}',reinforcement_net_w_diameter='{26}',"
                        + "reinforcement_net_w_spacing='{27}',top_l_reinforcement_diameter='{28}',top_l_reinforcement_spacing='{29}',top_reinforcement_layers_number='{30}',"
                        + "top_w_reinforcement_diameter='{31}',top_w_reinforcement_spacing='{32}'"
                        + " where id='{0}'", _row["id"].ToString(),
                  
                       this.around_h_reinforcement_diameter.Text, this.around_h_reinforcement_spacing.Text, this.around_l_reinforcement_diameter.Text,
                       this.around_reinforcement_spacing.Text, this.around_w_reinforcement_diameter.Text, this.bottom_l_reinforcement_diameter.Text,
                       this.bottom_l_reinforcement_spacing.Text, this.bottom_reinforcement_layers_number.Text, this.bottom_w_reinforcement_diameter.Text,
                       this.bottom_w_reinforcement_spacing.Text,
                       this.concrete_no.Text, this.concrete_protect_layer.Text, this.connector_spacing.Text,
                       this.erection_reinforcement_diameter.Text, this.erection_reinforcement_spacing.Text,
                       this.height.Text, this.horizontal_support_l_reinforcement_diameter.Text, this.horizontal_support_reinforcement_layers_number.Text,
                       this.horizontal_support_reinforcement_spacing.Text, this.horizontal_support_w_reinforcement_diameter.Text, this.length.Text,
                       this.pile_penetration_depth.Text, this.reinforcement_grade.Text, this.reinforcement_net_l_diameter.Text,
                       this.reinforcement_net_l_spacing.Text, this.reinforcement_net_w_diameter.Text, this.reinforcement_net_w_spacing.Text,
                       this.top_l_reinforcement_diameter.Text, this.top_l_reinforcement_spacing.Text, this.top_reinforcement_layers_number.Text,
                       this.top_w_reinforcement_diameter.Text, this.top_w_reinforcement_spacing.Text);

                    //暂不更新，算量计算后期和服务端关联
                    MySqlHelper.ExecuteNonQuery(updatesql, CommandType.Text);

                    PileCapParameter para = ModelOper.GetPileCapPara(_row["id"].ToString(), out _row);
                    // 钢筋明细
                    string selectString = string.Format("SELECT * FROM t_reinforcement_summary where part_type='{0}' and parent_id='{1}' and status=0  order by no asc", 1, _row["id"].ToString());
                    System.Data.DataTable dt1 = MySqlHelper.ExecuteDataTable(selectString, System.Data.CommandType.Text);

                    // 材料明细
                    selectString = string.Format("SELECT * FROM t_material_summary where part_type='{0}' and parent_id='{1}' and calculate_type='{2}' and status=0 ", 1, _row["id"].ToString(), 1);
                    System.Data.DataTable dt2 = MySqlHelper.ExecuteDataTable(selectString, System.Data.CommandType.Text);
                    int OverAllScale = (int)_scale;
             
                    PileCap A = new PileCap(para);

                    Database db = HostApplicationServices.WorkingDatabase;
                    A.DrawingPileCap(db, OverAllScale, dt1, dt2);
                }
            }
        }
    }
}
